<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="20%"
    >
      <el-form-item label="所属域" prop="domainId">
        <el-select v-model="dataForm.domainId" placeholder="请选择">
          <el-option v-for="(domain, index) in domainList" :label="domain.name" :value="domain.id" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="配置名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="配置名称"></el-input>
      </el-form-item>
      <el-form-item label="SDN控制器IP" prop="sdnIp">
        <el-input v-model="dataForm.sdnIp" placeholder="SDN控制器IP"></el-input>
      </el-form-item>
      <el-form-item label="SDN控制器端口" prop="sdnPort">
        <el-input v-model="dataForm.sdnPort" placeholder="SDN控制器端口"></el-input>
      </el-form-item>
      <el-form-item label="SDN的类型" prop="sdnType">
        <el-select v-model="dataForm.sdnType" placeholder="SDN的类型">
          <el-option label="华为" value="huawei"></el-option>
          <el-option label="中兴" value="zhongxing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="sdn控制器厂商用户名" prop="sdnUserName">
        <el-input v-model="dataForm.sdnUserName" placeholder="sdn控制器厂商用户名,无填0"></el-input>
      </el-form-item>
      <el-form-item label="sdn控制器厂商用户名密码" prop="sdnUserPswd">
        <el-input v-model="dataForm.sdnUserPswd" placeholder="sdn控制器厂商用户名密码,无填0"></el-input>
      </el-form-item>
      <el-form-item label="配置描述" prop="configDesc">
        <el-input type="textarea" v-model="dataForm.configDesc" placeholder="配置描述"></el-input>
      </el-form-item>
      <el-form-item label="告警阈值" prop="threshold">
        <el-input v-model="dataForm.threshold" placeholder="告警阈值"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        domainId: "",
        sdnIp: "",
        sdnPort: "",
        sdnType: "",
        sdnUserName: "",
        sdnUserPswd: "",
        configDesc: "",
        threshold: ""
      },
      domainList: [],
      dataRule: {
        domainId: [
          { required: true, message: "请选择所属域", trigger: "blur" }
        ],
        name: [
          { required: true, message: "请输入配置名称", trigger: "blur" }
        ],
        sdnIp: [
          { required: true, message: "SDN控制器IP不能为空", trigger: "blur" }
        ],
        sdnPort: [
          { required: true, message: "SDN控制器端口不能为空", trigger: "blur" }
        ],
        sdnType: [
          { required: true, message: "SDN的类型不能为空", trigger: "blur" }
        ],
        sdnUserName: [
          {
            required: true,
            message: "sdn控制器厂商用户名不能为空",
            trigger: "blur"
          }
        ],
        sdnUserPswd: [
          {
            required: true,
            message: "sdn控制器厂商用户名密码不能为空",
            trigger: "blur"
          }
        ],
        configDesc: [
          { required: true, message: "配置描述不能为空", trigger: "blur" }
        ],
        threshold: [
          { required: true, message: "告警阈值不能为空", trigger: "blur" }
        ]
      }
    };
  },
  activated() {
    this.getDomain();
  },
  methods: {
    getDomain() {
      this.domainList = []
      this.$http({
        url: this.$http.adornPortalUrl("/generator/domain/list"),
        method: "get",
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        if (data && data.code === 0) {
          let list = data.domains;
          for (let i = 0; i < list.length; i++) {
            this.domainList.push({
              id: list[i].domainId,
              name: list[i].domainName
            });
          }
        }
      });
    },
    init(id) {
      this.getDomain();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornEndogenousUrl(
              `/sdnconfig/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.sdnIp = data.sdnConfig.sdnIp;
              this.dataForm.name = data.sdnConfig.name;
              this.dataForm.domainId = data.sdnConfig.domainId;
              this.dataForm.sdnPort = data.sdnConfig.sdnPort;
              this.dataForm.sdnType = data.sdnConfig.sdnType;
              this.dataForm.sdnUserName = data.sdnConfig.sdnUserName;
              this.dataForm.sdnUserPswd = data.sdnConfig.sdnUserPswd;
              this.dataForm.configDesc = data.sdnConfig.configDesc;
              this.dataForm.threshold = data.sdnConfig.threshold;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornEndogenousUrl(
              `/sdnconfig/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              domainId: this.dataForm.domainId,
              name: this.dataForm.name,
              sdnIp: this.dataForm.sdnIp,
              sdnPort: this.dataForm.sdnPort,
              sdnType: this.dataForm.sdnType,
              sdnUserName: this.dataForm.sdnUserName,
              sdnUserPswd: this.dataForm.sdnUserPswd,
              configDesc: this.dataForm.configDesc,
              threshold: this.dataForm.threshold
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
